package com.aliyun.heiyu.powermaster.server.rotary.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.heiyu.common.util.DateUtil;
import com.aliyun.heiyu.powermaster.server.rotary.object.dto.CommonAJDTO;
import com.aliyun.heiyu.powermaster.server.rotary.object.transit.ResTem;
import com.aliyun.heiyu.powermaster.server.rotary.service.IpcService;
import com.aliyun.heiyu.powermaster.server.rotary.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description ipc实现类
 * @Author shun.zhang
 * @Date 2019/12/5 15:32
 **/
public class IpcServiceImpl implements IpcService {
    private static Logger logger = LoggerFactory.getLogger(IpcServiceImpl.class);

    @Override
    public ResTem sendOrderToIpc(String ipcUrl, String order) {
        //加密
        CommonAJDTO commonAJDTO = new CommonAJDTO();
        commonAJDTO.setContent(order);
        commonAJDTO.setSign("123");
        commonAJDTO.setTimestamp(System.currentTimeMillis());
        //TODO 1.sign加密
        //TODO 2.base64加密
        String request = JSONObject.toJSONString(commonAJDTO, true);
        logger.info("request:"+request);
        org.json.JSONObject jsonObject = HttpUtil.postRequest(request, ipcUrl);
        if (null == jsonObject) {
            return null;
        }
        CommonAJDTO commonAJ = JSONObject.parseObject(jsonObject.toString(), CommonAJDTO.class);
        logger.info("post请求获取到的数据:"+commonAJDTO.toString());
        //对数据进行保存
        return JSONObject.parseObject(commonAJ.getContent(), ResTem.class);
    }

    /**
     * 对返回的数据进行切割处理
     *
     * @param
     * @return
     **/
    public void saveRecordTime(String order, CommonAJDTO commonAJDTO) {
        //对返回的时间进行切割处理
    }

    public Object[] getTimeStep(String[] recordTimes, String order) {
        if (recordTimes.length == 4) {
            Object[] params = new Object[5];
            params[0] = order;
            for (int i = 0; i < recordTimes.length; i++) {
                Long step = Long.parseLong(recordTimes[3 - i]);
                if (i == 0) {
                    params[4 - i] = DateUtil.getmicTime() - step;
                } else {
                    params[4 - i] = Long.parseLong(recordTimes[4 - i]) - step;
                }
            }
            return params;
        }
        return null;
    }
}
